﻿using System.Windows.Forms;
using System;

namespace AllInOne
{
    public class HomePage
    {
        HtmlDocument doc;
        HtmlElement leftPanel;        
        HtmlElement btnRun;       
        HtmlElement lblSample;       
        HtmlElement txtContent;
        HtmlElement htmlOutput;

        public HomePage(HtmlDocument document)
        {
            this.doc = document;
            leftPanel= doc.GetElementById("leftPanel");
            btnRun= doc.GetElementById("btnRun");
            lblSample=doc.GetElementById("lbSample");
            txtContent=doc.GetElementById("txtContent");
            htmlOutput=doc.Window.Frames["txtOutput"].Document.Body;
        }

        public void AddSampleToList(string name, Action<string> clickHandler)
        {
            var element = doc.CreateElement("div");
            element.InnerHtml = name;
            element.Click += (o, e1) =>
            {
                clickHandler((o as HtmlElement).InnerHtml);
            };
            doc.GetElementById("leftPanel").AppendChild(element);
        }

        public void AddRunHandler(Action action) {
            btnRun.Click += (o, e) => action();
        }

        public string LableSample
        {
            get { return lblSample.InnerText; }
            set { lblSample.InnerText = value; }
        }

        public string TextContent
        {
            get { return txtContent.InnerText; }
            set { txtContent.InnerText = value; }
        }

        public string HtmlOutput
        {
            get { return htmlOutput.InnerHtml; }
            set { htmlOutput.InnerHtml = value; }
        }

        /// <summary>
        /// Executes an Active Scripting function defined in an HTML page.
        /// </summary>
        /// <param name="method">The name of the script method to invoke.</param>
        /// <param name="args">The arguments to pass to the script method.</param>
        /// <returns>The object returned by the Active Scripting call.</returns>
        public object Invoke(string method, params object[] args)
        {
            return this.doc.InvokeScript(method, args);
        }
    }
}
